import java.awt.Rectangle;

import javax.swing.JComboBox;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * 
 *is the control class of Add new book Gui.
 */
public class AddSubjectControl
{
	/** array of strings for add fields */
	public String[] searchData = new String[3]; 
	/** array of strings for o_GetSubjects sent info */
	public String[] searchData2 = new String[2]; 
	/** array of strings for o_AddSubject */
	public String[] subjectData = new String[4];
	/** array of strings for o_GetSubjects result*/
	public String[] res;
	/** array of strings for other results*/
	public String Answer;
	
/**
	 * this method Collects needed info from gui and sends DB a request to add a subject for a field
	 */
	public void  o_AddSubject(AddSubjectGui SubGui) 
	{
		Answer=new String();
		subjectData=new String[4];
		subjectData[0]=Main.gusers.id;
		subjectData[1]="addSubject";
		subjectData[2]=SubGui.SubjectText.getText();
		subjectData[3]=(String) SubGui.FieldCombo.getSelectedItem();
		if(subjectData[2].equals(""))
    	{
    		 JOptionPane.showMessageDialog(null,"Subject is empty, Please try again !","Input error",JOptionPane.ERROR_MESSAGE);
    		 return;
    	 }
		Main.log.connection.accept("addSubject");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		Answer=ChatClient.getRs();
		if(Answer.equals("Exists"))
		{
			JOptionPane.showMessageDialog(null,"The entered Subject '"+subjectData[2]+"' already exists for the selected Field '"+subjectData[3]+"'" +" !","Database Conflict",JOptionPane.ERROR_MESSAGE);
			
		}
		else if(Answer.equals("OK"))
		{
			JOptionPane.showMessageDialog(null,"Subject Succesfully added to Selected Field in Database","Database Success",JOptionPane.INFORMATION_MESSAGE);
		}
		o_Back(SubGui.panel);
	}
	/**
	 * this method Collects needed info from gui and sends DB a request to Remove a subject for a field
	 * Cant remove if a book is asociated to the selected subject and field
	 */
	public void  o_RemoveSubject(AddSubjectGui SubGui) 
	{
		Answer=new String();
		subjectData=new String[4];
		subjectData[0]=Main.gusers.id;
		subjectData[1]="RemoveSubject";
		subjectData[2]=(String)SubGui.SubjectCombo.getSelectedItem();
		subjectData[3]=(String) SubGui.FieldCombo2.getSelectedItem();		
		Main.log.connection.accept("RemoveSubject");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;
		Answer=ChatClient.getRs();
		if(Answer.equals("Exists"))
			JOptionPane.showMessageDialog(null,"A book Exists in Database for The entered Subject: '"+subjectData[2]+"'  and Field: '"+subjectData[3]+"'" +" !\n Can't remove a subject with related books","Database Conflict",JOptionPane.ERROR_MESSAGE);		
		else if(Answer.equals("OK"))
				JOptionPane.showMessageDialog(null,"Subject Succesfully Removed from Selected Field in Database","Database Success",JOptionPane.INFORMATION_MESSAGE);
		o_Back(SubGui.panel);   
	}
	
	/**
	 * this method Collects needed info from gui and sends DB a request to retreive Fields
	 * if num=1 retreive all fields,
	 * if num=2 retreive only fields for selected subject
	 * updates the gui Combo box with the reteived result
	 */
	public void  o_GetFields(AddSubjectGui SubGui, int num) 
	{
		String temp;	
		searchData = new String[3];
		searchData[0]=Main.gusers.id;
		if (num==1) 
			searchData[1]="getFields";
		else if (num==2)
		{
			searchData[2]=(String)SubGui.SubjectCombo.getSelectedItem();
			searchData[1]="getFieldsSubject";
		}	
		Main.log.connection.accept(searchData[1]);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		res=temp.split("\\*");
		if (num==1)		// first field combo
		{
			if (SubGui.FieldCombo == null) {
				SubGui.FieldCombo = new JComboBox(res);				
				SubGui.FieldCombo.setBounds(new Rectangle(130, 140, 100, 36));
			}		
			SubGui.panel.add(SubGui.FieldCombo,null);
		}
		else 		//second Field Combo
		{
			if (SubGui.FieldCombo2 == null) {
				SubGui.FieldCombo2 = new JComboBox(res);				
				SubGui.FieldCombo2.setBounds(new Rectangle(130, 290, 100, 36));
			}
			SubGui.FieldCombo2.updateUI();
			SubGui.panel.add(SubGui.FieldCombo2,null);
			
		}
		SubGui.panel.updateUI();		
	}
	/**
	 * this method Collects needed info from gui and sends DB a request to retreive Subjects
	 * updates the gui Combo box with the reteived result
	 */
	public void  o_GetSubjects(AddSubjectGui SubGui) 
	{
		String temp;
		searchData2=new String[2];
		searchData2[0]=Main.gusers.id;
		searchData2[1]="getSubjects";				
		Main.log.connection.accept("getSubjects");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		temp=ChatClient.getRs();
		res=temp.split("\\*");		
		//for(i=0;i<this.res.length;i++)
		// System.out.println(this.res[i]);		
		if (SubGui.SubjectCombo == null) {
			SubGui.SubjectCombo = new JComboBox(res);				
			SubGui.SubjectCombo.setBounds(new Rectangle(130, 250, 100, 36));
		}		
		SubGui.panel.add(SubGui.SubjectCombo,null);
		SubGui.panel.updateUI();		
	}
	/**
	 * this method Goes back to the last Gui Screen - Book Management
	 */	
	public void o_Back(JPanel panel) 
	{
		panel.setVisible(false);
		panel.setEnabled(false);
		Main.log.frame.setSize(550,515);
		Main.bookManagement.panel.setVisible(true);
		Main.bookManagement.panel.setEnabled(true);
		
	}
	/**
	 * this method logs out of the system
	 */	
	public void o_logout(JPanel BookMngpanel)
	{
		if(JOptionPane.showConfirmDialog(null, "Are you sure you want to logout? ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION) == 0)
		{
			BookMngpanel.setEnabled(false);
			BookMngpanel.setVisible(false);
			Main.log.loginData = new String[3];
			Main.log.loginData[0]=Main.gusers.id;
			Main.log.loginData[1]="logout";
			Main.log.loginData[2]=Main.gusers.id;		
			Main.log.connection= new ClientQuery(Main.serverIP,5555);
			Main.log.connection.accept("logout");
			Main.log.user.setText("");
			Main.log.pass.setText("");
			Main.log.frame.setSize(650,550);
			Main.log.panel.setEnabled(true);
			Main.log.panel.setVisible(true);
		}
	}

}
